package ar.edu.unlam.tpfinal.struts.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import ar.edu.unlam.tpfinal.model.TransactionType;
import ar.edu.unlam.tpfinal.service.TransactionService;
import ar.edu.unlam.tpfinal.struts.forms.TransactionForm;

public class TransactionEditAction extends Action {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
								 HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServlet().getServletContext());
		TransactionService ts = (TransactionService) context.getBean("transactionService");
		TransactionForm transactionFormForEdit = new TransactionForm();
		List<LabelValueBean> priorityComboBox;
		List<LabelValueBean> clientListMultibox;
		List<LabelValueBean> transactionTypeComboBox;
		List<LabelValueBean> statusComboBox;
		if(form!=null && ((TransactionForm)form).getOid()!=null && ((TransactionForm)form).getOid().longValue()!=0){
			TransactionForm transactionForm = (TransactionForm)form;
			transactionFormForEdit = ts.getTransactionByOID(transactionForm.getOid());
			
		}
		
		clientListMultibox = ts.getAllClients();
		if(clientListMultibox==null){
			clientListMultibox = new ArrayList<LabelValueBean>();
		}
		
		priorityComboBox = ts.getAllPriorities();
		if(priorityComboBox==null){
			priorityComboBox = new ArrayList<LabelValueBean>();
		}
		
		transactionTypeComboBox = ts.getAllTransactionTypes();
		
		statusComboBox = ts.getAllStatus();
		
		request.setAttribute("statusComboBox", statusComboBox);
		request.setAttribute("transactionTypeComboBox", transactionTypeComboBox);
		request.setAttribute("priorityComboBox", priorityComboBox);
		request.setAttribute("clientListMultibox", clientListMultibox);
		
		request.setAttribute("transactionForm", transactionFormForEdit);
		
		return mapping.findForward("success");
	}
}
